package com.sulent.frame.file.test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.util.Formatter;

/**
 * MessageDigest 是 Java 中用于生成消息摘要（哈希值）的类。它是 java.security 包中的一个抽象类，提供了一些方法来处理消息摘要。
 */
public class MessageDigestExample {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
/*        String input = "Hello, world!";
        String md5Hash = getMD5Hash(input);
        System.out.println("MD5 hash: " + md5Hash);*/

        String s = calculateFileMD5("D:\\docker-image\\1637662097027665922.tar");
        System.out.println(s);
        String s1 = calculateFileSHA256("D:\\docker-image\\1637662097027665922.tar");
        System.out.println(s1);

        long max = 1024L *1024*1024*1024;
        System.out.println(max);

        String u1 = "http://172.16.32.23:30021/datacenter/asset-file/multi-upload/20240206163612_1754786053286580225.docx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=jIWdnS7x6g9dWMAY%2F20240219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240219T075118Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=e8508d91808c6579b3267f903feb68671c94a408a7eec90fe1f783a2de15e4f";
        String u2 = "http://172.16.32.23:30021/datacenter/asset-file/multi-upload/20240206163612_1754786053286580225.docx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=jIWdnS7x6g9dWMAY%2F20240219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240219T082045Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=8acb15eb708bcf196acbc42278adf0019a7779ef5fb4d14c287df43b8ab22157";
        String u3 = "http://172.16.48.10:31002/datacenter/asset-file/multi-upload/20240206163612_1754786053286580225.docx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=jIWdnS7x6g9dWMAY%2F20240219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240219T082045Z&X-Amz-Expires=7200&X-Amz-SignedHeaders=host&X-Amz-Signature=8acb15eb708bcf196acbc42278adf0019a7779ef5fb4d14c287df43b8ab22157";
    }

    public static String getMD5Hash(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
            byte[] digestBytes = md.digest(inputBytes);
            return bytesToHex(digestBytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static String calculateFileSHA256(String filePath) throws IOException, NoSuchAlgorithmException {
        // 创建MessageDigest实例
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // 读取文件内容
        Path path = Paths.get(filePath);
        byte[] fileContent = Files.readAllBytes(path);

        // 更新MessageDigest实例
        md.update(fileContent);

        // 计算哈希值并格式化为十六进制字符串
        byte[] digest = md.digest();
        Formatter formatter = new Formatter();
        for (byte b : digest) {
            formatter.format("%02x", b);
        }
        String sha256 = formatter.toString();
        formatter.close();

        return sha256;
    }


    public static String calculateFileMD5(String filePath) throws IOException, NoSuchAlgorithmException {
        // 创建MessageDigest实例
        MessageDigest md = MessageDigest.getInstance("MD5");

        // 读取文件内容
        Path path = Paths.get(filePath);
        byte[] fileContent = Files.readAllBytes(path);

        // 更新MessageDigest实例
        md.update(fileContent);

        // 计算哈希值并格式化为十六进制字符串
        byte[] digest = md.digest();
        Formatter formatter = new Formatter();
        for (byte b : digest) {
            formatter.format("%02x", b);
        }
        String md5 = formatter.toString();
        formatter.close();

        return md5;
    }
}